package com.example.music_liwei.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.music_liwei.R;
import com.example.music_liwei.activity.MusicPlayerActivity;
import com.example.music_liwei.adapter.FirstBaseQuickAdapter;
import com.example.music_liwei.bean.ResponseData;
import com.example.music_liwei.interceptor.MyInterceptor;
import com.example.music_liwei.messageEvent.MessageEvent;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.greenrobot.eventbus.EventBus;

import java.io.IOException;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class BlankFragment2 extends Fragment {

    private static final String TAG = "ning";
    private RecyclerView recyclerView;
    private OkHttpClient okHttpClient;
    FirstBaseQuickAdapter adapter;
    List<ResponseData.MusicInfo> musicInfoList;
    private Intent intent;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(new MyInterceptor())
                .build();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank1, container, false);
        recyclerView = view.findViewById(R.id.recyclerView1);

        intent = new Intent(getActivity(), MusicPlayerActivity.class);

        search("/music/homePage", 1, 5,2);




        return view;
    }

    ////////////////////////////我在这封装一个方法，便于查找////////////////////////////////////////////////////////////
    public void search(String path, int current, int size,int tyeNum) {
        String url = "https://hotfix-service-prod.g.mi.com" + path + "?current=" + current + "&size=" + size;

        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();

        okHttpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(@NonNull Call call, @NonNull IOException e) {
                Log.d(TAG, "onFailure: 网络请求失败", e);
            }

            @Override
            public void onResponse(@NonNull Call call, @NonNull Response response) throws IOException {
                if (response.isSuccessful() && response.body() != null) {
                    String jsonResponse = response.body().string();
                    Log.d(TAG, "onResponse: 网络请求成功 code " + response.code());

                    Gson gson = new Gson();
                    Type responseType = new TypeToken<ResponseData>() {}.getType();
                    ResponseData responseData = gson.fromJson(jsonResponse, responseType);

                    musicInfoList = new ArrayList<>();
                    for (ResponseData.Record record : responseData.getData().getRecords()) {
                        if (record.getStyle() == tyeNum) {
                            musicInfoList.addAll(record.getMusicInfoList());
                            break; //         ////////////////////////////////////////////////////////////////////////
                        }
                    }

                    if (getActivity() != null) {
                        getActivity().runOnUiThread(() -> {
                            adapter = new FirstBaseQuickAdapter(R.layout.item_frag3, musicInfoList);

                            recyclerView.setLayoutManager(new LinearLayoutManager(getActivity(),LinearLayoutManager.HORIZONTAL,false));
                            recyclerView.setAdapter(adapter);


                            // 设置点击事件
                            adapter.setOnItemClickListener(new OnItemClickListener() {
                                @Override
                                public void onItemClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                                    EventBus.getDefault().postSticky(new MessageEvent(musicInfoList)); // 发布事件
                                    if (getActivity() != null) {
                                        startActivity(intent);
                                    }
                                }
                            });
                        });
                    }
                } else {
                    Log.d(TAG, "onResponse: 网络请求失败 code " + response.code());
                }
            }
        });
    }

}